//
//  ONAppDelegate.m
//  Volatix
//
//  Created by Familly on 6/2/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "ONAppDelegate.h"
#import "FBEncryptorAES.h"
@implementation ONAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // status tint color
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    // remove hightlight for tabbar
    [[UIView appearance] setTintColor:[UIColor whiteColor]];
    // Override point for customization after application launch.
    sleep(2);
   

    
    // enable Push notification
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];
    NSLog(@"ENABLE PUSH NOTIFICATION");
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    return YES;
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString * deviceTokenString =[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    deviceTokenString =[deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *devicetoken=[self gettingDeviceToken];
    NSLog(@"deviceTokenString  %@",deviceTokenString);
    if(![devicetoken isEqualToString:deviceTokenString]||devicetoken==nil)
    {
        NSLog(@"saving  %@",deviceTokenString);
        [self savingDeviceToken:deviceTokenString];
    }
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"fail to get token , error %@",error);
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"user infor %@",userInfo);
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
-(NSString *)gettingDeviceToken
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    return [prefs objectForKey:@"DEVICE_TOKEN"];
}
-(void)savingDeviceToken:(NSString *)devicetoken
{
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:devicetoken forKey:@"DEVICE_TOKEN"];
    [defaults synchronize];
    
}
-(NSMutableArray*)getingDataSymbleForTypeScreen:(int)value
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedUser = [defaults objectForKey:(value==0)?@"DATA_REAL_TIME":(value==1)?@"DATA_DAY_AHEAD":@"DATA_ALERT"];
    NSData* decryptedData = [FBEncryptorAES decryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    NSMutableArray *obj = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData: decryptedData];
    return obj;
    
}
-(void)savingDataSymble:(NSMutableArray*)array andTypeScreen:(int)value//RealTime
{
    NSData *encodedUser = [NSKeyedArchiver archivedDataWithRootObject:array];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData* encryptedData = [FBEncryptorAES encryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    [defaults setObject:encryptedData forKey:(value==0)?@"DATA_REAL_TIME":(value==1)?@"DATA_DAY_AHEAD":@"DATA_ALERT"];
    [defaults synchronize];
}

-(NSMutableArray*)getingFilterTimerForTypeScreen:(int)value
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedUser = [defaults objectForKey:(value==0)?@"FILTER_REAL_TIMER":(value==1)?@"FILTER_DAY_AHEAD":@"FILTER_ALERT"];
    NSData* decryptedData = [FBEncryptorAES decryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    NSMutableArray *obj = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData: decryptedData];
    return obj;
    
}
-(void)savingFilterTimer:(NSMutableArray*)array andTypeScreen:(int)value
{
    NSData *encodedUser = [NSKeyedArchiver archivedDataWithRootObject:array];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData* encryptedData = [FBEncryptorAES encryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    [defaults setObject:encryptedData forKey:(value==0)?@"FILTER_REAL_TIMER":(value==1)?@"FILTER_DAY_AHEAD":@"FILTER_ALERT"];
    [defaults synchronize];
}

-(void)savingFilterBySymble:(NSMutableArray*)array andTypeScreen:(NSString *)key
{
    NSData *encodedUser = [NSKeyedArchiver archivedDataWithRootObject:array];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData* encryptedData = [FBEncryptorAES encryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    [defaults setObject:encryptedData forKey:key];
    [defaults synchronize];
}
-(NSMutableArray*)getingFilterBySymble:(NSString *)key
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedUser = [defaults objectForKey:key];
    NSData* decryptedData = [FBEncryptorAES decryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    NSMutableArray *obj = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData: decryptedData];
    return obj;
    
}


-(NSMutableArray*)getingCurrentColumnNameRealTime
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedUser = [defaults objectForKey:@"COLUMN_REAL_TIME"];
    NSData* decryptedData = [FBEncryptorAES decryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    NSMutableArray *obj = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData: decryptedData];
    return obj;
    
}
-(void)savingCurrentColumnNameRealTime:(NSMutableArray*)array
{
    NSData *encodedUser = [NSKeyedArchiver archivedDataWithRootObject:array];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData* encryptedData = [FBEncryptorAES encryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    [defaults setObject:encryptedData forKey:@"COLUMN_REAL_TIME"];
    [defaults synchronize];
}

///// Name Column
-(NSMutableArray*)getingListDynamicColumnNameRealTime:(BOOL)value
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedUser = [defaults objectForKey:(value)?@"NAME_COLUMN_DYNAMIC_REAL_TIME":@"NAME_COLUMN_DYNAMIC_DAY_AHEAD"];
    NSData* decryptedData = [FBEncryptorAES decryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    NSMutableArray *obj = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData: decryptedData];
    return obj;
    
}
-(void)savingListDynamicColumnName:(NSMutableArray*)array andRealTime:(BOOL)value
{
    NSData *encodedUser = [NSKeyedArchiver archivedDataWithRootObject:array];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData* encryptedData = [FBEncryptorAES encryptData:encodedUser key:[APP_KEY dataUsingEncoding:NSUTF8StringEncoding] iv:nil];
    [defaults setObject:encryptedData forKey:(value)?@"NAME_COLUMN_DYNAMIC_REAL_TIME":@"NAME_COLUMN_DYNAMIC_DAY_AHEAD"];
    [defaults synchronize];
}


@end
